class Solution:
    def isHappy(self, n: int) -> bool:
        def get_next(n):
            res = 0
            while n > 0:
                res += (n % 10) ** 2
                n //= 10
            return res
        slow = n
        fast = get_next(n)
        while slow != fast:
            slow = get_next(slow)
            fast = get_next(get_next(fast))
        return slow == 1